package com.caiwangdai.app.extension

import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions.withCrossFade
import com.bumptech.glide.request.RequestOptions


//显示
fun View.visible() {
    this.visibility = View.VISIBLE
}

//隐藏-占位
fun View.invisible() {
    this.visibility = View.INVISIBLE
}

//隐藏-不占位
fun View.gone() {
    this.visibility = View.GONE
}

//设置宽度和高度
fun View.setViewLayoutParams(width: Int, height: Int) {
    val p = this.layoutParams
    if (width >= 0) {
        p.width = width
    }
    if (height >= 0) {
        p.height = height
    }
    this.layoutParams = p
}

//设置高度
fun View.setHeight(height: Int) {
    this.setViewLayoutParams(-1, height)
}

//设置宽度
fun View.setWidth(width: Int) {
    this.setViewLayoutParams(width, -1)
}

//设置外边界
fun View.setMargins(l: Int, t: Int, r: Int, b: Int) {
    if (this.layoutParams is ViewGroup.MarginLayoutParams) {
        val p = this.layoutParams as ViewGroup.MarginLayoutParams
        p.setMargins(l, t, r, b)
        this.requestLayout()
    }
}


//请求获取焦点
fun View.requestFocus() {
    this.isFocusable = true
    this.isFocusableInTouchMode = true
    this.requestFocus()
}

//请求去除焦点
fun View.clearFocus(view: View) {
    view.isFocusable = false
    view.isFocusableInTouchMode = false
    view.clearFocus()
}

/**
 * 修改字体样式
 */
fun ViewGroup.changeFonts() {

    val tf = Typeface.createFromAsset(context.getAssets(), "font/HelveticaNeue-Light.ttf")
    for (i in 0..this.childCount - 1) {
        val v = this.getChildAt(i)
        if (v is TextView) {
            v.typeface = tf
        } else if (v is Button) {
            v.typeface = tf
        } else if (v is EditText) {
            v.typeface = tf
        } else if (v is ViewGroup) {
            v.changeFonts()
        }
    }

}


//解析xml视图
fun ViewGroup.inflate(layoutRes: Int): View {
    return LayoutInflater.from(context).inflate(layoutRes, this, false)
}


//图片加载
var glideOptions = RequestOptions()
        .centerCrop()



fun ImageView.loadUrl(url: String) {
    Glide.with(context).asBitmap()
            .load(url)
            .apply(glideOptions)
            .transition(withCrossFade())
            .into(this)
}

//防止双重点击
private var lastClickTime: Long = 0


fun View.setOnClickListenerPro(singleClick: ((View) -> Unit)) {
    this.setOnClickListenerPro(singleClick, null)
}


fun View.setOnClickListenerPro(singleClick: ((View) -> Unit), doubleClick: ((View) -> Unit)? = null, delay: Long = 700) {
    this.setOnClickListener {
        val start = System.currentTimeMillis()
        val diff = start - lastClickTime
        if (diff in 1..delay) {
            doubleClick?.invoke(this)
        } else {
            lastClickTime = start
            singleClick.invoke(this)
        }
    }
}

fun View.setOnClickListenerPro(singleClick: View.OnClickListener) {
    this.setOnClickListener {
        val start = System.currentTimeMillis()
        val diff = start - lastClickTime
        if (diff in 1..600) {
            return@setOnClickListener
        }
        lastClickTime = start
        singleClick.onClick(this)
    }
}














